package com.Jhu1.controller;

import com.Jhu1.entity.Material;
import com.Jhu1.entity.PerOrder;
import com.Jhu1.service.PerOrderService;
import com.Jhu1.util.PageVo;
import com.Jhu1.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private PerOrderService orderService;
    
    @GetMapping("/all")
    public Result<List<PerOrder>> getOrderList() {
        List<PerOrder> orders = orderService.getAll();
        return Result.success(orders);
    }
    
    @GetMapping("/allByPage")
    public Result<PageVo<PerOrder>> getByPage(
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("pageSize") Integer pageSize
    ) {
        PageVo<PerOrder> pv = orderService.allByPage(pageNum, pageSize);
        return Result.success(pv);
    }
    
    @PatchMapping("/handle")
    public Result<?> handle(@RequestParam("id") Integer id, @RequestParam("state") boolean state) {
        orderService.handle(id, state);
        return Result.success();
    }
    
    @PutMapping("/submit")
    public Result<?> submit(@RequestBody List<Material> materials) {
        orderService.submit(materials);
        return Result.success();
    }
    
    @GetMapping("/myOrder")
    public Result<List<PerOrder>> myOrder() {
        List<PerOrder> orders = orderService.myOrder();
        return Result.success(orders);
    }
    
    @GetMapping("/allByState")
    public Result<List<PerOrder>> allByState(@RequestParam("state") boolean state) {
        return Result.success(orderService.allByState(state));
    }
}
